package com.wcgksoft.controller.agent;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.admin.Authority;
import com.wcgksoft.entity.system.Enterprise;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.admin.AccountService;
import com.wcgksoft.service.admin.AuthorityService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.agent
 * @Description TODO 代理商 管理员给代理商开账号
 * @date 2020/5/14 14:47
 * @ClassName AgentController
 */
@Controller
@RequestMapping("system/agent")
public class AgentController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AuthorityService authorityService;

    /**
     * 查询所有代理商的列表
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit,
                         @RequestParam(value = "name",required = false)String name){
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class);
        criteria.add(Restrictions.eq("isJurisdiction",0));
        if (StringUtils.isNotEmpty(name)){
            criteria.add(Restrictions.or(Restrictions.like("name","%"+name+"%", MatchMode.ANYWHERE),
                    Restrictions.like("phone","%"+name+"%", MatchMode.ANYWHERE),
                    Restrictions.like("address","%"+name+"%", MatchMode.ANYWHERE)));
        }
        criteria.addOrder(Order.desc("createDate"));
        List<Account> list = accountService.findByConditions(criteria,page,limit);
        return Layui.data(0, "success", accountService.getTotalCount(), list);
    }

    /**
     * 开设账号
     * */
    @ResponseBody
    @RequestMapping(value = "openAccount")
    public Layui openAccount(HttpServletRequest request,
                             @RequestParam(value = "data", required = false)String data)
    {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null)
        {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            Account account = (Account) JSONObject.toBean(JSONObject.fromObject(data), Account.class);
            String id=(String)param.get("id");
            if ("".equals(id)) {
                Authority authority = authorityService.findById(2);
                account.setAuthority(authority);
                account.setStatus(1);
                account.setIsJurisdiction(0);
                account.setUserNumber(0);
                account.setSaleNumber(0);
                BigDecimal bigDecimal = new BigDecimal("0");
                account.setWithdrawalMoney(bigDecimal);
                BigDecimal bigDecimal1 = new BigDecimal("0");
                account.setSaleMoney(bigDecimal1);
                account.setCreateDate(new Date());
                accountService.save(account);
            }else{
                Authority authority = authorityService.findById(2);
                account.setAuthority(authority);
                account.setStatus(1);
                account.setIsJurisdiction(0);
                accountService.saveMerge(account);
            }

            return Layui.data(0, "开设账号成功",1, Layui.addList(account));
        }
    }


    /**
     * 删除代理商
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false)Integer id){
        Account account = accountService.findById(id);
        accountService.delete(account);
        return Layui.data(0, "success", 1, null);
    }

    /**
     * 取消代理权限
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "chanel")
    public Layui chanel(@RequestParam(value = "id",required = false)Integer id){
        Account account = accountService.findById(id);
        account.setIsJurisdiction(1);
        accountService.saveMerge(account);
        return Layui.data(0, "success", 1, null);
    }


    /**
     * 根据代理商id 找到业务员列表
     * @param page
     * @param limit
     * @param agent_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAgentSaleMan")
    public Layui findAgentASaleMan(@RequestParam(value = "page",required = false)Integer page,
                                   @RequestParam(value = "limit",required = false)Integer limit,
                                   @RequestParam(value = "agent_id",required = false)Integer agent_id){
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class);
        criteria.add(Restrictions.eq("agent_id",agent_id));
        List<Account> list = accountService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", accountService.getTotalCount(), list);
    }
}
